package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.DoctorPatientGroupDTO;
import com.pshcp.domain.model.DoctorPatientGroup;
import com.pshcp.mapper.DoctorPatientGroupMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医生的患者分组 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorPatientGroupService {

    @Autowired
    private DoctorPatientGroupMapper doctorPatientGroupMapper;

    public PageList<DoctorPatientGroup> list(DoctorPatientGroupDTO doctorPatientGroupDTO) {
        if (doctorPatientGroupDTO.getPageNum() != null && doctorPatientGroupDTO.getPageSize() != null) {
            PageHelper.startPage(doctorPatientGroupDTO.getPageNum(), doctorPatientGroupDTO.getPageSize());
        }
        Example example = new Example(DoctorPatientGroup.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorPatientGroupDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorPatientGroupDTO.getStatus());
        } else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if (doctorPatientGroupDTO.getDoctorId() != null) {
            criteria.andEqualTo("doctorId", doctorPatientGroupDTO.getDoctorId());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorPatientGroup> list = doctorPatientGroupMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorPatientGroup> select(DoctorPatientGroup doctorPatientGroup) {
        return doctorPatientGroupMapper.select(doctorPatientGroup);
    }

    public Long insert(DoctorPatientGroup doctorPatientGroup) {
        doctorPatientGroup.setId(SystemUtil.getId());
        doctorPatientGroup.setStatus(CommonStatusEnum.INIT.getCode());
        doctorPatientGroup.setCreatedTime(new Date());
        doctorPatientGroupMapper.insert(doctorPatientGroup);
        return doctorPatientGroup.getId();
    }

    public int update(DoctorPatientGroup doctorPatientGroup) {
        doctorPatientGroup.setUpdateTime(new Date());
        return doctorPatientGroupMapper.updateByPrimaryKeySelective(doctorPatientGroup);
    }

    public DoctorPatientGroup findById(Long id) {
        return doctorPatientGroupMapper.selectByPrimaryKey(id);
    }

    public List<DoctorPatientGroup> findByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return Collections.emptyList();
        }
        Example example = new Example(DoctorPatientGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        return doctorPatientGroupMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorPatientGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorPatientGroup doctorPatientGroup = new DoctorPatientGroup();
        doctorPatientGroup.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorPatientGroupMapper.updateByExampleSelective(doctorPatientGroup, example);
    }

}
